package com.zst.springbootjsonintegrationdemo.entities.movie.entity;

import com.zst.springbootjsonintegrationdemo.converter.ListToJsonStringConverter;
import com.zst.springbootjsonintegrationdemo.converter.MapToJsonStringConverter;

import javax.persistence.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qq578
 */
@Entity
@Table(name = "movie", indexes = @Index(name = "type_idx", columnList = "dateInMap"))
public class Movie {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "genre", columnDefinition = "json")
    private String genre;

    @Column(name = "type", columnDefinition = "varchar(255) generated always as (genre->>'$.type') virtual", insertable = false, updatable = false)
    private String type;

    @Column(name = "date", columnDefinition = "datetime generated always as (genre->>'$.date') virtual", insertable = false, updatable = false)
    private Date date;

    @Column(name = "mapToJson", columnDefinition = "json")
    @Convert(converter = MapToJsonStringConverter.class)
    private Map<String, String> mapToJson;

    @Column(name = "listToJson", columnDefinition = "json")
    @Convert(converter = ListToJsonStringConverter.class)
    private List<String> listToJson;

    @Column(name = "dateInMap", columnDefinition = "datetime generated always as (map_to_json->>'$.date') virtual", insertable = false, updatable = false)
    private Date dateInMap;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Map<String, String> getMapToJson() {
        return mapToJson;
    }

    public void setMapToJson(Map<String, String> mapToJson) {
        this.mapToJson = mapToJson;
    }

    public List<String> getListToJson() {
        return listToJson;
    }

    public void setListToJson(List<String> listToJson) {
        this.listToJson = listToJson;
    }

    public Date getDateInMap() {
        return dateInMap;
    }

    public void setDateInMap(Date dateInMap) {
        this.dateInMap = dateInMap;
    }

    @Override
    public String toString() {
        return "Movie{" +
                "id=" + id +
                ", genre='" + genre + '\'' +
                ", type='" + type + '\'' +
                ", date=" + date +
                ", mapToJson=" + mapToJson +
                ", listToJson=" + listToJson +
                '}';
    }
}
